package com.haiyou.data.common.persister.mysql.help.invoke;

import java.util.concurrent.atomic.AtomicInteger;

import com.alibaba.fastjson2.util.TypeUtils;
import com.haiyou.common.entity.Entity;
import com.haiyou.data.common.persister.mysql.help.ColumnHelper;
import com.haiyou.data.common.persister.mysql.meta.ColumnMeta;

public class IntHelper implements InvokeHelper {

	@Override
	public void invoke(ColumnMeta columnMeta, Object ov, Entity<?> entity) {
		Integer v = TypeUtils.cast(ov,Integer.class);
		try {
			if (ColumnHelper.getGenericType(columnMeta) == AtomicInteger.class) {
				ColumnHelper.getSetter(columnMeta).invoke(entity, new AtomicInteger(v.intValue()));
			} else if (ColumnHelper.getGenericType(columnMeta) == Integer.class) {
				ColumnHelper.getSetter(columnMeta).invoke(entity, v);
			} else if (ColumnHelper.getGenericType(columnMeta) == int.class) {
				ColumnHelper.getSetter(columnMeta).invoke(entity, v.intValue());
			} else {
				throw new RuntimeException("unknown:" + columnMeta);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage() + ":" + ov + ",\n" + entity + ",\n" + columnMeta, e);
		}
	}

}
